<?php

namespace App\Service\Guanyuan;

use App\Remote\Guanyuan\GuanyuanV3\GuanyuanRemoteV3;
use App\Service\OpenShop\Feishu\ConfigService;
use Exception;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class CurlService
{
    /**
     * @var bool 是否强制刷新
     */
    private bool $forceRefresh = false;

    public function __construct()
    {
        $this->init();
    }

    private array $dataToken = [];

    // 数据集
    private const GW_BASE_PUBLIC_API_URL = 'https://kuafu.guandatacloud.com/public-api/';

    // 初始化观远年数据获取
    private function init()
    {
        $this->dataToken = ConfigService::config("guanyuanData")['dataToken'];
    }

    /**
     * 观远请求接口处 增加缓存处理
     *
     * @param string $dsId
     * @param array $requestData
     * @return array|mixed
     */
    public function dataSourceCurl(string $dsId = '', array $requestData = [])
    {
        $cacheKey = $dsId . md5(json_encode($requestData));
        if ($this->forceRefresh) {
            $cacheKey .= time();
        }

        return Cache::remember($cacheKey, 600,
            function () use ($dsId, $requestData) {
                $dataSource = $this->dataToken[$dsId];

                $url = vsprintf('data-source/%s/token/%s', [$dataSource['dsId'], $dataSource['token']]);

                $requestUrl = self::GW_BASE_PUBLIC_API_URL . $url;

//                dd($requestUrl);
//                return Http::withHeaders(['Content-Type' => 'application/json; charset=utf-8'])
//                           ->post($requestUrl, $requestData)
//                           ->json();
                return GuanyuanRemoteV3::guanyuanRemoteV3(
                    $requestUrl,
                    RequestAlias::METHOD_POST,
                    $requestData,
                    ["Content-Type" => 'application/json']
                );
            });
    }

    public function dataSourceBySource(string $dsId, string $token, array $requestData = [])
    {
        $cacheKey = $dsId . md5(json_encode($requestData));
        if ($this->forceRefresh) {
            $cacheKey .= time();
        }

        return Cache::remember($cacheKey, 600,
            function () use ($dsId, $requestData, $token) {
                $url = vsprintf('data-source/%s/token/%s', [$dsId, $token]);

                $requestUrl = self::GW_BASE_PUBLIC_API_URL . $url;

//                return Http::withHeaders(['Content-Type' => 'application/json; charset=utf-8'])
//                           ->post($requestUrl, $requestData)
//                           ->json();
                return GuanyuanRemoteV3::guanyuanRemoteV3(
                    $requestUrl,
                    RequestAlias::METHOD_POST,
                    $requestData,
                    ["Content-Type" => 'application/json']
                );
            });
    }


    private const DATA_SOURCE_ID_SIGN_ORDER_ROBOT           = 'orderSignRobot'; // 签约机器人
    private const DATA_SOURCE_ID_ANNUAL_SHOP_ROBOT_V2       = 'annualShopV2'; // 周表机器人 V2
    private const DATA_SOURCE_ID_SHOP_STATUS_WEEK_ROBOT     = 'shopStatusWeek'; // 周表机器人
    private const DATA_SOURCE_ID_SHANG_ZENG_DAILY_ROBOT     = 'shopShangZengResults'; // 商增业绩数据源
    private const DATA_SOURCE_ID_MONTH_FINISH_PERCENT_ROBOT = 'monthFinishPercent'; // 签约完成率数据集
    private const DATA_SOURCE_ID_SIGN_ORDER_PERSONAL_ROBOT  = 'orderSignPersonalRobot'; // 全员招商优秀个人
    private const DATA_SOURCE_ID_SIGN_ORDER_TEAM_ROBOT      = 'orderSignTeamRobot'; // 全员招商优秀分公司
    private const DATA_SOURCE_ID_MONTH_COMPLETE_ROBOT       = 'monthCompleteRobot'; // 本月签约完成
    private const DATA_SOURCE_ID_MONTH_NOTICE_ROBOT         = 'monthRobot'; // 招商业绩提醒机器人
    private const DATA_SOURCE_ID_SITE_SHOP_ROBOT            = 'siteShopRobot'; // 选址状态门店数
    private const DATA_SOURCE_ID_PREPARATIONS_SHOP_ROBOT    = 'preparationsShopRobot'; // 筹备&新开完成情况
    private const DATA_SOURCE_ID_MARK_PEN_RATE_ROBOT        = 'marketPenetrationRateRobot'; // 商场渗透率
    private const DATA_SOURCE_ID_MONTH_SITE_NEW_SHOP_ROBOT  = 'monthSiteNewShopRobot'; // 月度确址&新签门店数
    private const DATA_SOURCE_ID_DAY_SHOP_GRADE_ROBOT       = 'dayShopGradeRobot'; // 日门店分级占比
    private const DATA_SOURCE_ID_DAY_DAU_REAL_ROBOT         = 'dayDauRealRobot'; // 日DAU、实收数据
    private const DATA_SOURCE_ID_MONTH_DORMANCY_SHOP_ROBOT  = 'monthDormancyShopRobot'; // 上月休眠门店数


    /**
     * 年度目标筛选数据
     *
     * @param array $result
     * @return mixed
     * @throws Exception
     */
    public function getFormatAnnualShopData()
    {
        $field      = [
            '新开门店目标'        => 'newOpenTargetNumMonth',
            '本月完成率'         => 'newOpenTargetNumMonthPercent',
            '在营门店数'         => 'stillOpenShopNum',
            '当月在营目标'        => 'stillOpenShopTargetNumMonth',
            '本月新开门店'        => 'newOpenShopNumMonth',
            '本年新开门店'        => 'newOpenShopNumYear',
            '省份'            => 'province',
            '全年闭店'          => 'closedShopNumLimitYear', // 一年关店上限
            '2022年底在营门店数合计' => 'gwStillOpenShopTargetNumYear'  // 本年在营目标
        ];
        $result     = $this->getAnnualShopByFilter($field);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }

    /**
     * 门店状态周数据 - 筛选过后数据
     *
     * @return mixed
     * @throws Exception
     */
    public function getFormatShopStatusWeek()
    {
        $field = [
            '区域'   => 'area',
            '省份修正' => 'province',
            '本月新开' => 'newOpenShopNumMonthGyWeek',
            '本周新开' => 'newOpenShopNumWeek',
            '本周在营' => 'stillOpenShopNumWeek',
            '本周新签' => 'newSignGWNumWeek',
            '本月新签' => 'newSignGWNumMonth',
            '本月在营' => 'stillOpenShopNumMonth',
            '累计营业' => 'sumOpenShopNum',
            '本年闭店' => 'closedShopNumYear',
            '本周筹备' => 'preparationsNumWeek'
        ];

        $result = $this->getShopStatusWeek($field);

        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }

    /**
     * 门店状态周数据 - 原数据
     *
     * @return array|mixed
     */
    public function getShopStatusWeek(array $field)
    {
        return $this->dataSourceCurl(self::DATA_SOURCE_ID_SHOP_STATUS_WEEK_ROBOT,
            ['cols' => array_keys($field)]);

    }

    /**
     * 年度目标数据 - 原数据
     *
     * @return array|mixed
     */
    public function getAnnualShopByFilter(array $field)
    {
        $filter =
            [
                [
                    'name'        => '月份',
                    'filterType'  => 'EQ',
                    'filterValue' => [date('Y-m-d', strtotime(date('Y-m')))]
                ]
            ];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        return $this->dataSourceCurl(self::DATA_SOURCE_ID_ANNUAL_SHOP_ROBOT_V2, $requestForm);
    }


    /**
     * 获取业绩分组数据
     *
     * @return mixed
     * @throws Exception
     */
    public function getShangZengDailyData()
    {
        $field  = [
            '回款日期' => 'gyRemitDate',
            '组别'   => 'gyGroup',
            '客户数'  => 'gyCustomerNum',
            '门店数'  => 'gyShopNum',
            '回款'   => 'gyRemitMoney'
        ];
        $filter =
            [
                [
                    'name'        => '回款日期',
                    'filterType'  => 'EQ',
                    'filterValue' => [date('Y-m-d', strtotime(date('Y-m-d')))]
//                    'filterValue' => ['2022-08-08']
                ]
            ];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_SHANG_ZENG_DAILY_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }

    // 夸父区域督导
    // https://kuafu.guandatacloud.com/data-source/t3581b0579e50436e99110e7/details/overview?limit=50
    public function getCompanyList()
    {
        $field = [
            '区域'   => 'area',
            '省区总'  => 'newSignShopNum',
            '省份'   => 'province',
            '城市经理' => 'cityManager',
            '区域经理' => 'areaManager',
            '督导'   => 'councilor'
        ];

        $filter =
            [

            ];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceBySource('t3581b0579e50436e99110e7', 'd4106c1885bad4916bd152aa', $requestForm);
        $formatData = $this->formatData($result, $field);

        return $formatData['response']['data'];
    }

    public function getMonthSign()
    {
        $field  = [
            '区域'    => 'area',
            '签约门店数' => 'newSignShopNum',
        ];
        $filter =
            [
                [
                    'name'        => '回款月份',
                    'filterType'  => 'EQ',
                    'filterValue' => [date('Y-m-01', strtotime(date('Y-m-d')))]
                ]
            ];


        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceBySource('g93eb867cee184018b88136b', 'oe492441ae6e742b180a6ff2', $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }

    public function getZhaoShangGroup()
    {
        $field  = [
            '组别' => 'groupName',
            '排序' => 'sort'
        ];
        $filter =
            [
            ];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceBySource('aff4a9b3c95bb408bb0220be', 'v195c4312bfd24cc190e7de9', $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }


    /**
     * 获取签约完成率
     *
     * @return mixed
     * @throws Exception
     */
    public function getGYQianYuePercent()
    {
        $field  = [
            '目标月份'      => 'gyMonth',
            '新签约客户数目标'  => 'gyNewSignNum',
            '本月签约客户数'   => 'gySignNumMonth',
            '本月签约门店数'   => 'gySignShopMonth',
            '本月签约目标达成率' => 'gySignPercentMonth'
        ];
        $filter =
            [
                [
                    'name'        => '目标月份',
                    'filterType'  => 'EQ',
                    'filterValue' => [date('Y-m-d', strtotime(date('Y-m') . '-01'))]
                ]
            ];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_MONTH_FINISH_PERCENT_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }


    /**
     * 签约机器人
     *
     * @param array $result
     * @return mixed
     * @throws Exception
     */
    public function getFormatOrderSignRobot(string $date)
    {
        $field      = [
            '类型'   => 'typeGySignRobot',
            '回款日期' => 'leadTimeGySignRobot',
            '加盟顾问' => 'joinConsultantGySignRobot',
            '省'    => 'provinceGySignRobot',
            '市'    => 'cityGySignRobot',
            '门店数'  => 'shopCountGySignRobot',

        ];
        $result     = $this->getOrderSignRobot($field, $date);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }

    /**
     * 签约机器人 - 原数据
     *
     * @return array|mixed
     */
    public function getOrderSignRobot(array $field, string $date)
    {
        $filter =
            [
                [
                    'name'        => '回款日期',
                    'filterType'  => 'EQ',
                    'filterValue' => [$date]
                ]
            ];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result = $this->dataSourceCurl(self::DATA_SOURCE_ID_SIGN_ORDER_ROBOT, $requestForm);

        return $result;
    }

    /**
     * 基础数据清洗
     *
     * @param array $result
     * @param array $field
     * @return array
     * @throws Exception
     */
    private function formatData(array $result, array $field)
    {
        Log::info('debug_formatData_start：', $result);
        if (empty($result) || $result['result'] != 'ok') {
            throw new Exception('请求观远数据失败' . PHP_EOL . json_encode($result));
        }


        $fieldMap = [];
        foreach ($result['response']['columns'] as $key => $column) {
            if (key_exists($column, $field)) {
                $fieldMap[$column] = $key;
            }
        }

        foreach ($result['response']['data'] as &$datum) {
            foreach ($fieldMap as $fieldKey => $fieldValue) {
                $datum[$field[$fieldKey]] = $datum[$fieldValue];
                unset($datum[$fieldValue]);
            }
        }

        unset($datum);
        Log::info('debug_formatData_end：', $result);
        return $result;
    }

    /**
     * @return bool
     */
    public function isForceRefresh(): bool
    {
        return $this->forceRefresh;
    }

    /**
     * @param bool $forceRefresh
     */
    public function setForceRefresh(bool $forceRefresh): void
    {
        $this->forceRefresh = $forceRefresh;
    }

    /**
     * 全员招商优秀个人
     *
     * @param string $date
     * @return mixed
     * @throws Exception
     */
    public function getFormatOrderSignPersonalRobot(string $date)
    {
        $field = [
            '回款月份' => 'month',
            '签约人'  => 'inviterName',
            '分公司'  => 'departmentName',
            '裂变数量' => 'fission',
            '拉新数量' => 'new',
            '签约总数' => 'total',
            '奖金'   => 'reward',
        ];
        $result     = $this->getOrderSignPersonalRobot($field, $date);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }

    /**
     * 全员招商优秀个人 - 原数据
     * @param array $field
     * @param string $date
     * @return array|mixed
     */
    public function getOrderSignPersonalRobot(array $field, string $date): array
    {
        $filter =
            [
                [
                    'name'        => '回款月份',
                    'filterType'  => 'EQ',
                    'filterValue' => [$date]
                ]
            ];
        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];
        return $this->dataSourceCurl(self::DATA_SOURCE_ID_SIGN_ORDER_PERSONAL_ROBOT, $requestForm);
    }


    /**
     * 全员招商优秀分公司
     *
     * @param string $date
     * @return mixed
     * @throws Exception
     */
    public function getFormatOrderSignTeamRobot(string $date)
    {
        $field      = [
            '回款月份' => 'month',
            '分公司'  => 'departmentName',
            '裂变数量' => 'fission',
            '拉新数量' => 'new',
            '签约总数' => 'total',
            '奖金'   => 'reward',
        ];
        $result     = $this->getOrderSignTeamRobot($field, $date);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }

    /**
     * 全员招商优秀分公司 - 原数据
     * @param array $field
     * @param string $date
     * @return array|mixed
     */
    public function getOrderSignTeamRobot(array $field, string $date): array
    {
        $filter =
            [
                [
                    'name'        => '回款月份',
                    'filterType'  => 'EQ',
                    'filterValue' => [$date]
                ]
            ];
        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];
        return $this->dataSourceCurl(self::DATA_SOURCE_ID_SIGN_ORDER_TEAM_ROBOT, $requestForm);
    }

    /**
     * 本月签约完成率 - 原数据 https://kuafu.guandatacloud.com/data-source/kc132e456197b4a6a8f58d30/details/overview?limit=50
     * @param string $date
     * @return mixed
     * @throws Exception
     */
    public function getFormatMonthSigningCompletionRobot(string $date)
    {
        $field = [
            '目标月份'      => 'month',
            '本月签约门店数'   => 'newShopCount',
            '新签约门店数目标'  => 'targetShopCount',
            '本月签约客户数'   => 'newCustomerCount',
            '本月签约目标达成率' => 'rate',
        ];

        $filter =
            [
                [
                    'name'        => '目标月份',
                    'filterType'  => 'EQ',
                    'filterValue' => [$date]
                ]
            ];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_MONTH_COMPLETE_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }

    /**
     * 招商业绩提醒机器人 - 原数据 https://kuafu.guandatacloud.com/data-source/w841763cee13b4a62a640097/details/overview
     * @param string $date
     * @return mixed
     * @throws Exception
     */
    public function getFormatMonthSigningCompletionNoticeRobot(string $date)
    {
        $field = [
            '月份'    => 'month',
            '招商'    => 'name',
            '已完成业绩' => 'complete',
            '剩余业绩'  => 'remain',
        ];

        $filter =
            [
                [
                    'name'        => '月份',
                    'filterType'  => 'EQ',
                    'filterValue' => [$date]
                ]
            ];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_MONTH_NOTICE_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }


    /**
     * 选址状态门店数 - 原数据 https://kuafu.guandatacloud.com/data-source/de2a338f2e6604aa88dbcee3/details/overview
     * @return mixed
     * @throws Exception
     */
    public function getFormatSiteShopRobotData()
    {
        $field = [
            '区域'  => 'branch',
            '省份'  => 'province',
            '城市'  => 'city',
            '子状态' => 'status',
            '门店数' => 'number',
        ];

        $filter = [];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_SITE_SHOP_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }


    /**
     * 筹备&新开完成情况 - 原数据 https://kuafu.guandatacloud.com/data-source/pd1d447700e7b4121acb119a/details/overview?limit=50
     * @return mixed
     * @throws Exception
     */
    public function getFormatPreparationsShopRobotData()
    {
        $field = [
            '区域'     => 'branch',
            '省份'     => 'province',
            '城市'     => 'city',
            '总筹备门店数' => 'preTotalNumber',
            '异常筹备总数' => 'preAbnormalNumber',
            '正常筹备总数' => 'preNormalNumber',
            '当月待开'   => 'monthPreNumber',
            '本月筹备正常' => 'monthPreNormalNumber',
            '本月异常筹备' => 'monthPreAbnormalNumber',
            '本月预计新开' => 'monthDoNewNumber',
            '本月新开目标' => 'monthDoNewTarget',
            '本月实际新开' => 'monthRealNewNumber',
            '本月新开差额' => 'monthNewDiffNumber',
        ];

        $filter = [];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_PREPARATIONS_SHOP_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }


    /**
     * 商场渗透率 - 原数据 https://kuafu.guandatacloud.com/data-source/e6f3bd38cbf5347a88036296/details/overview?limit=50
     * @return mixed
     * @throws Exception
     */
    public function getFormatMarkPenRateRobotData()
    {
        $field = [
            '区域'    => 'branch',
            '省份'    => 'province',
            '城市'    => 'city',
            '商场等级' => 'grade',
            '入驻数量'  => 'settledNumber',
            '商场数量'  => 'number',
        ];

        $filter = [];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_MARK_PEN_RATE_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }


    /**
     * 月度确址&新签门店数 - 原数据 https://kuafu.guandatacloud.com/data-source/fc4d40e75942d4865919a9c3/details/overview?limit=50
     * @return mixed
     * @throws Exception
     */
    public function getFormatMonthSiteNewShopRobotData()
    {
        $field = [
            '区域'      => 'branch',
            '省份'      => 'province',
            '城市'      => 'city',
            '本月新签门店'  => 'newSignShop',
            '本月确址门店数' => 'newSiteShop',
            '本月签约目标'  => 'newSignTarget',
            '本月线索量'   => 'clueNum',
        ];

        $filter = [];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_MONTH_SITE_NEW_SHOP_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }


    /**
     * 日门店分级占比 - 原数据 https://kuafu.guandatacloud.com/data-source/eaf26a04a99bd47f895afe88/details/overview?limit=50
     * @return mixed
     * @throws Exception
     */
    public function getFormatDayShopGradeRobotData()
    {
        $field = [
            '区域'   => 'branch',
            '省份'   => 'province',
            '城市'   => 'city',
            '门店分级' => 'grade',
            '门店数'  => 'number',
        ];

        $filter = [];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_DAY_SHOP_GRADE_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }


    /**
     * 日DAU、实收数据 - 原数据 https://kuafu.guandatacloud.com/data-source/x2e2f9fcf1786414e8ad0bd4/details/overview?limit=50
     * @return mixed
     * @throws Exception
     */
    public function getFormatDayDauRealShopRobotData()
    {
        $field = [
            '区域'     => 'branch',
            '省份'     => 'province',
            '城市'     => 'city',
            'DAU门店数' => 'dau',
            'GMV'    => 'gmv',
            '实收金额'   => 'totalAmount',
            '外卖实收金额' => 'totalWaimaiAmount',
            'WAU'    => 'wau',
            'MAU'    => 'mau',
            'AU'     => 'au',
            '堂食实收金额' => 'totalCanteenAmount',
        ];

        $filter = [];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_DAY_DAU_REAL_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }
    /**
     * 上月休眠门店数 - 原数据 https://kuafu.guandatacloud.com/data-source/ia0cbfef42e2041629d13bfd/details/overview
     * @return mixed
     * @throws Exception
     */
    public function getFormatMonthDormancyShopRobotData()
    {
        $field = [
            '区域'      => 'branch',
            '省份'      => 'province',
            '城市'      => 'city',
            '上月休眠门店数' => 'dormancyShopNum',
            '上月MAU'   => 'mau',
            '城市分类'   => 'cityType',
        ];

        $filter = [];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceCurl(self::DATA_SOURCE_ID_MONTH_DORMANCY_SHOP_ROBOT, $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }




    /**
     * 开发当月任务数 - 原数据 https://kuafu.guandatacloud.com/data-source/df0aa691b09fa4b349c04263/details/overview?limit=50
     * @return mixed
     * @throws Exception
     */
    public function getFormatMonthDevelopRankRobotData()
    {
        $field = [
            '区域'     => 'branch',
            '开发经理本名' => 'developManagerRealName',
            '当月任务数'  => 'taskNum',
            '本月确址门店' => 'siteShop',
            '昵称'     => 'nickname',
            '开发经理'   => 'developManagerName',
        ];

        $filter = [];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceBySource('df0aa691b09fa4b349c04263', 'r5b4a480ca67742e88a2d0b3', $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }


    /**
     * 筹备异常明细 - 原数据 https://kuafu.guandatacloud.com/data-source/d47561205e0754f418d361d7/details/overview?limit=50
     * @return mixed
     * @throws Exception
     */
    public function getFormatMonthPreAbnormalRobotData()
    {
        $field = [
            '区域'     => 'branch',
            '省份'      => 'province',
            '城市'      => 'city',
            '门店编号'   => 'shopCode',
            '门店名称'   => 'shopName',
            '最新阶段'   => 'latestStage',
            '预计开业时间'   => 'expectedOpeningTime',
            '异常原因'   => 'reason',
            '详情描述'   => 'desc',
            '加盟商姓名'   => 'name',
        ];

        $filter = [];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceBySource('d47561205e0754f418d361d7', 's897233a9d84d4144b638151', $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }

    /**
     * 生炸鸡腿数据 观远数据源链接：https://kuafu.guandatacloud.com/data-source/d64928106d1ab4b728c57c4f/details/overview
     * @param string $date
     * @return mixed
     * @throws Exception
     */
    public function getFriedChickenLegRobotData(string $date)
    {
        $field = [
            '当天订货数'   => 'orderGoodsDayNum',
            '当天订货门店数' => 'orderStoreDayNum',
            '堂食实收'    => 'actualIncome',
            '外卖实收'    => 'takeawayActualIncome',
            '堂食gmy'   => 'gmv',
            '外卖gmv'   => 'takeawayGmv',
            '堂食销量'    => 'salesVolume',
            '外卖销量'    => 'takeawaySalesVolume',
            '省份'      => 'province',
            '日期'      => 'date',
            '累计订货数'   => 'orderGoodsTotalNum',
            '累计订货门店'  => 'orderStoreTotalNum',
            '本月在营'    => 'shopNum',
            '门店渗透率'   => 'shopPenetrationRate',
        ];

        $filter =
            [
                [
                    'name'        => '日期',
                    'filterType'  => 'EQ',
                    'filterValue' => [$date]
                ]
            ];

        $requestForm = [
            'cols'    => array_keys($field),
            'filters' => $filter
        ];

        $result     = $this->dataSourceBySource('d64928106d1ab4b728c57c4f', 'h30393e8ccd9147fe9363de3', $requestForm);
        $formatData = $this->formatData($result, $field);
        return $formatData['response']['data'];
    }
}
